/*
 * Copyright 2011 Jin Kwon <jinahya at gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.googlecode.flac4j.format.metadata;


import com.googlecode.flac4j.format.FormatHelper;

import com.googlecode.jinahya.io.BitInput;
import com.googlecode.jinahya.io.BitOutput;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.util.Arrays;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;


/**
 *
 * @author Jin Kwon <jinahya at gmail.com>
 */
@XmlType(propOrder = {"comment", "commentAsASCII", "commentAsUTF8"})
public class VorbisComment extends AbstractData {


    /**
     * Creates a new instance.
     *
     * @param comment comment
     * @return a new instance.
     */
    public static Data newInstance(final byte[] comment) {

        if (comment == null) {
            throw new NullPointerException("null comment");
        }

        final VorbisComment instance = new VorbisComment();
        instance.comment = comment;

        return instance;
    }


    public VorbisComment() {
        super(BlockType.VORBIS_COMMENT);
    }


    @Override
    public int getLengthInBytes() {
        return comment.length;
    }


    @Override
    public void read(final BitInput input) throws IOException {

        comment = new byte[getHeader().getDataLength()];
        FormatHelper.readBytes(input, comment);
    }


    @Override
    public void write(final BitOutput output) throws IOException {

        FormatHelper.writeBytes(output, comment);
    }


    /**
     * Returns comment.
     *
     * @return comment
     */
    public byte[] getComment() {
        return Arrays.copyOf(comment, comment.length);
    }


    /**
     * Sets comment.
     *
     * @param comment comment
     */
    public void setComment(final byte[] comment) {

        if (comment == null) {
            throw new NullPointerException("null comment");
        }

        this.comment = Arrays.copyOf(comment, comment.length);
    }


    // ------------------------------------------------------------------- ASCII
    @XmlElement
    public String getCommentAsASCII() throws UnsupportedEncodingException {
        return new String(getComment(), "US-ASCII");
    }


    public void setCommentAsASCII(final String comment)
        throws UnsupportedEncodingException {

        setComment(comment.getBytes("US-ASCII"));
    }


    // ------------------------------------------------------------------- UTF-8
    @XmlElement(name = "commentAsUTF-8")
    public String getCommentAsUTF8() throws UnsupportedEncodingException {
        return new String(getComment(), "UTF-8");
    }


    public void setCommentAsUTF8(final String comment)
        throws UnsupportedEncodingException {

        setComment(comment.getBytes("UTF-8"));
    }


    /** comment. */
    @XmlElement(required = true)
    @XmlSchemaType(name = "base64Binary")
    private byte[] comment;


}

